package com.luck.easyfoodbatch.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.dialog.CustomDialog;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdateBindPhoneActivity extends BaseActivity {

    @BindView(R.id.rl_phone_tab1)
    RelativeLayout rlPhoneTab1;
    @BindView(R.id.rl_phone_tab2)
    RelativeLayout rlPhoneTab2;
    private Timer tm;
    private TimerTask tk;
    private int count = 60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_bind_phone, "修改绑定手机", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rl_phone_tab1, R.id.rl_phone_tab2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_phone_tab1:
                showSendMsgDialog();
                break;
            case R.id.rl_phone_tab2:
                break;
        }
    }


    /**
     * 发送验证码
     */
    private TextView tv_send_msg;

    private void showSendMsgDialog() {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth * 3 / 4,
                LinearLayout.LayoutParams.WRAP_CONTENT, R.layout.wind_send_msg_dialog_xml, R.style.Theme_dialog);
        Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        tv_send_msg = (TextView) dialog.findViewById(R.id.tv_send_msg);
        final EditText et_tel = (EditText) dialog.findViewById(R.id.et_tel);
        tv_content.setText("输入手机尾号135****3723收到的验证码");
        /** 3.自动弹出软键盘 **/
        et_tel.requestFocus();
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            public void onShow(DialogInterface dialog) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(et_tel, InputMethodManager.SHOW_IMPLICIT);
                    }
                }, 50);

            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (tk != null && tm != null) {
                    tk.cancel();
                    tm.cancel();
                }
                dialog.dismiss();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (tk != null && tm != null) {
                    tk.cancel();
                    tm.cancel();
                }
                dialog.dismiss();
            }
        });
        tv_send_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startCount();
            }
        });
        dialog.show();
    }

    /**
     * 启动时间器读秒
     */
    private void startCount() {
        tm = new Timer();
        tk = new TimerTask() {

            @Override
            public void run() {
                Message msg = mHandler.obtainMessage();
                count--;
                if (count > 0) {
                    msg.arg1 = count;
                    msg.what = 0;
                    msg.obj = count;
                    mHandler.sendMessage(msg);
                } else {
                    // 时间读完 初始秒数
                    mHandler.sendEmptyMessage(11);
                    count = 60;
                }

            }
        };
        tm.schedule(tk, 0, 1000);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    tv_send_msg.setEnabled(false);
                    tv_send_msg.setText(msg.arg1 + "秒");
                    break;
                case 11:
                    tv_send_msg.setEnabled(true);
                    tv_send_msg.setText("发送验证码");
                    tk.cancel();
                    break;
            }
        }

    };
}
